/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "ESearchResultParser.h"

#include <QtCore/QIODevice>
#include <QtCore/QCoreApplication>
#include <QtXml/QDomDocument>

#include "ESearch.h"

using namespace Bio;

///////////////////////////////////////////////////////////////////////////////
//	ESearchResultParser
///////////////////////////////////////////////////////////////////////////////

/**
	@class ESearchResultParser
	@brief Parser for Entrez search results
**/

/**
	Default Constructor. Creates a new parser for the given ESearch operation.
**/
ESearchResultParser::ESearchResultParser( ESearch &search ) : m_search( search )
{
}

/**
**/
ESearchResultParser::~ESearchResultParser()
{
}

/**
**/
bool ESearchResultParser::parse( QIODevice &dev )
{
	QDomDocument	doc;

	if( !doc.setContent( &dev ) ){
		return false;
	}

	bool		status = true;
	QDomNode	node = doc.firstChild();

	while( !node.isNull() ){
		QDomElement	elem = node.toElement();

		if( !elem.isNull() ){
			if( elem.tagName() == "eSearchResult" )
				parseESearchResult( elem );
			else {
				error( QString( "Expected 'eSearchResult', found '%1'." ).arg( elem.tagName() ) );
				status = false;
			}
		}

		node = node.nextSibling();
	}

	return status;
}

/**
**/
void ESearchResultParser::parseESearchResult( const QDomElement &root )
{
	QDomNode	node = root.firstChild();
	int			queryKey = -1;
	QString		queryTranslation, webEnv;

	while( !node.isNull() ){
		QDomElement	elem = node.toElement();

		if( !elem.isNull() ){
			QString	tagName = elem.tagName();

			if( tagName == "Count" )
				m_search.setCount( elem.text().toInt() );
			else if( tagName == "RetMax" )
				m_search.setRetMax( elem.text().toInt() );
			else if( tagName == "RetStart" )
				m_search.setRetStart( elem.text().toInt() );
			else if( tagName == "QueryKey" )
				queryKey = elem.text().toInt();
			else if( tagName == "WebEnv" )
				webEnv = elem.text();
			else if( tagName == "IdList" )
				parseIdList( elem );
			else if( tagName == "TranslationSet" )
				parseTranslationSet( elem );
			else if( tagName == "TranslationStack" )
				parseTranslationStack( elem );
			else if( tagName == "QueryTranslation" )
				queryTranslation = elem.text();
			else
				warning( QString( "Unexpected element in eSearchResult: " ) + tagName );
		}

		node = node.nextSibling();
	}

	if( queryKey >= 0 && !webEnv.isEmpty() )
		m_search.setHistory( queryKey, webEnv );
}

/**
**/
void ESearchResultParser::parseIdList( const QDomElement &root )
{
	QDomNode	node = root.firstChild();

	while( !node.isNull() ){
		QDomElement	elem = node.toElement();

		if( !elem.isNull() ){
			if( elem.tagName() == "Id" )
				m_search.addID( elem.text() );
			else
				warning( QString( "Unexpected element in IdList: " ) + elem.tagName() );
		}

		node = node.nextSibling();
	}
}

/**
**/
void ESearchResultParser::parseTranslationSet( const QDomElement &root )
{
	// todo
}

/**
**/
void ESearchResultParser::parseTranslationStack( const QDomElement &root )
{
	// todo
}
